jQuery(document).ready(function ($) {
    $('[data-toggle="tooltip"]').tooltip();
//
    if(window.location.href == "http://www.wmorz.com/site/index.html"){
        $(".wmhome").addClass("active");
    }
    if(window.location.href == "http://www.wmorz.com/focus/index.html"){
        $(".wmdy").addClass("active");
    }
    /*关注文章*/
    $(".collect").click(function () {
        var press = $(this);
        var id = press.children('i').html();
        $.ajax({
            url: collect_url,// 跳转到 action
            data: {
                id: id
            },
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function (data) {
                if (data.status == 1) {
                    console.log(data);
                    press.children('span').removeClass('glyphicon-heart').addClass('glyphicon-heart-empty').css('color', '#4a4a4a');
                    press.children('em').html("收藏").css('color', '#4a4a4a');
                } else if (data.status == 0) {
                    console.log(data);
                    press.children('span').removeClass('glyphicon-heart-empty').addClass('glyphicon-heart').css('color', 'red');
                    press.children('em').html("已收藏").css('color', 'red');
                } else if (data.status == -1) {
                    $('#myModal').modal('show');
                }
            },
            error: function () {
                console.log("关注异常！");
            }
        });
    });
    /*举报文章*/
    $(".report").click(function () {
        $('#reportModal').modal('toggle');
        /*设置隐藏域value*/
        var press = $(this);
        var id = press.children('i').html();
        $(".report_id").val(id);
        /*点击举报按钮时触发*/
        $(".report-btn").click(function () {
            $.ajax({
                url: report_url,
                data: $('#report-form').serialize(),
                type: 'post',
                cache: false,
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                    console.log(data);
                    if (data.status == 1) {
                        $('#reportModal').modal('hide');
                        alert(data.msg);
                    } else if (data.status == 0) {
                        $('#reportModal').modal('hide');
                        alert(data.msg);
                    }
                },
                error: function () {
                    console.log("举报异常！");
                }
            });
        });
    });
    /*订阅板块*/
    /*改变文字*/
    //jQuery(".fuckdy").hover(function(){
    //    $(this).text("取消订阅");
    //},function(){
    //    $(this).text("已订阅");
    //});
    //


    $(".dingyue").click(function () {
        var press = $(this);
        var id = press.children('i').html();
        $.ajax({
            url: dingyue_url,
            data: {
                id: id
            },
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function (data) {
                if (data.status == 1) {
                    console.log(data);
                    press.removeClass("btn-danger").addClass("btn-success");
                    press.children('span').html("+订阅");
                } else if (data.status == 0) {
                    console.log(data);
                    press.removeClass("btn-success").addClass("btn-danger fuckdy");
                    press.children('span').html("已订阅");
                } else if (data.status == -1) {
                    $('#myModal').modal('show');
                }
            },
            error: function () {
                console.log("订阅异常！");
            }
        });
    });


    /*赞和踩*/
    function zancai(fuck, type) {
        $(fuck).click(function () {
            var press = $(this);
            var parent = press.parents(".unvoted");
            //   console.log("-->"+total);
            var id = press.children('i').html();
            $.ajax({
                url: zancai_url,
                data: {
                    id: id,
                    type: type
                },
                type: 'post',
                cache: false,
                dataType: 'json',
                success: function (data) {
                    if (data.status == 1) {//点赞成功
                        press.addClass("active");
                       // alert(data.msg);
                        total = press.children(".num").text();
                     //   console.log(total);
                        press.children(".num").text(parseInt(total) + 1);
                        press.addClass("active");
                    } else if (data.status == -1) {
                        console.log(data);
                        alert(data.msg);
                    } else if (data.status == 2) {//点踩成功
                        press.addClass("active");
                       // alert(data.msg);
                        total = press.children(".num").text();
                      //  console.log(total);
                        press.children(".num").text(parseInt(total) + 1);
                        press.addClass("active");
                    } else if (data.status == 3) {//取消赞,点踩成功
                      //  alert(data.msg);
                        press.addClass("active");
                        total = press.children(".num").text();
                        press.children(".num").text(parseInt(total) + 1);

                        total2 = parent.children(".zan").children(".num").text();
                        parent.children(".zan").children(".num").text(parseInt(total2) - 1);
                        parent.children(".zan").removeClass("active");
                    } else if (data.status == 4) {//取消踩,赞踩成功
                      //  alert(data.msg);
                        press.addClass("active");
                        total = press.children(".num").text();
                        press.children(".num").text(parseInt(total) + 1);

                        total2 = parent.children(".cai").children(".num").text();
                        parent.children(".cai").children(".num").text(parseInt(total2) - 1);
                        parent.children(".cai").removeClass("active");
                    }
                },
                error: function () {
                    console.log("赞或者踩异常！");
                }
            });
        });
    }

    zancai(".zan", 0);
    zancai(".cai", 1);
    /*赞和踩*/
    /*签到*/

    $(".signin").click(function () {
        var press = $(this);
        $.ajax({
            url: signin_url,
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function (data) {
                if (data.status == 1) {
                    //  console.log(data);
                    press.removeClass("btn-info").removeClass("signin").addClass("btn-danger");
                    press.children('span').html("今 日 已 签 到");
                } else if (data.status == -1) {
                    //  console.log(data);
                }
            },
            error: function () {
                console.log("签到异常！");
            }
        });
    });

    /*文章摘要被点击时弹出模态对话框*/
    jQuery('.dialog').click(function () {
        var clickArea = $(this);
        jQuery('#content-dialog').modal({
            show: true
            //  remote: 'http://www.baidu.com'
        });
        jQuery('#content-dialog').on('shown.bs.modal', function (event) {

            var modal = jQuery("#content-dialog");
            var title = clickArea.children('#hide-title').text();
            var url = clickArea.children('#hide-url').text();
            var common = clickArea.children('#hide-comment').text();
            var commonnum = clickArea.children('#hide-comment').attr("data-num");
            console.log(commonnum);
            modal.find(".fuck-comment").text(commonnum+"个讨论");
            modal.find(".fuck-comment").attr("href",common);
            modal.find('.modal-header h2').text(title);
            modal.find('.modal-body iframe').attr('src', url);

        });
        $('#exampleModal').on('hidden.bs.modal', function (event) {
            location.reload();
        })
    });
    /*AJAX 评论*/

    jQuery("#comment-btn").click(function () {

        jQuery.ajax({
            cache: false,
            type: "post",
            url: comment_url,
            data: jQuery('#comment-form').serialize(),// 你的formid
            dataType: 'json',
            async: false,
            success: function (data) {
                if (data.status == 1) {
                    document.location.reload();
                } else if (data.status == -1) {
                    $('#anonymous-dialog').modal('show');
                }else if(data.status == -2){
                    alert(data.msg);
                }
            },
            error: function () {
                console.log("common error！");
            }
        });
    });


    /*折叠*/
$("[class = zhedie]").click(function () {

    var parent = $(this).parent().parent().parent() ;
    parent.find(".comment-bottom").toggleClass("hide");
    parent.find(".content").toggleClass("hide");
    parent.find(".media-left0").toggleClass("hide");

    var child =  parent.children(".comment_box");
    if (child) {
        child.toggleClass("hide");

    }

});

/*举报评论*/
    $("[id = report-comment]").click(function () {
        $('#reportCommentModal').modal('toggle');
        /*设置隐藏域value*/
        var press = $(this);
        var id = press.children('i').html();
        $(".comment_report_id").val(id);
        /*点击举报按钮时触发*/
        $(".report-btn").click(function () {
            $.ajax({
                url: comment_report_url,
                data: $('#report-comment-form').serialize(),
                type: 'post',
                cache: false,
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                    console.log(data);
                    if (data.status == 1) {
                        $('#reportCommentModal').modal('hide');
                        alert(data.msg);
                    } else if (data.status == 0) {
                        $('#reportCommentModal').modal('hide');
                        alert(data.msg);
                    }
                },
                error: function () {
                    console.log("举报异常！");
                }
            });
        });
    });
    /*评论赞和踩*/
    function commentzancai(fuck, type) {
        $(fuck).click(function () {
            var press = $(this);
            var id = press.attr("data-id");
            var parent = press.parents(".unvoted");
            //var score = press.attr("data-score");
            //var total = jQuery("#" + score + " b");
            $.ajax({
                url: commentzancai_url,
                data: {
                    id: id,
                    type: type
                },
                type: 'post',
                cache: false,
                dataType: 'json',
                    success: function (data) {
                        if (data.status == 1) {//点赞成功
                            press.addClass("active");
                            // alert(data.msg);
                            total = press.children(".num").text();
                            //   console.log(total);
                            press.children(".num").text(parseInt(total) + 1);
                            press.addClass("active");
                        } else if (data.status == -1) {
                            console.log(data);
                            alert(data.msg);
                        } else if (data.status == 2) {//点踩成功
                            press.addClass("active");
                            // alert(data.msg);
                            total = press.children(".num").text();
                            //  console.log(total);
                            press.children(".num").text(parseInt(total) + 1);
                            press.addClass("active");
                        } else if (data.status == 3) {//取消赞,点踩成功
                            //  alert(data.msg);
                            press.addClass("active");
                            total = press.children(".num").text();
                            press.children(".num").text(parseInt(total) + 1);

                            total2 = parent.children(".comment-zan").children(".num").text();
                            parent.children(".comment-zan").children(".num").text(parseInt(total2) - 1);
                            parent.children(".comment-zan").removeClass("active");
                        } else if (data.status == 4) {//取消踩,赞踩成功
                            //  alert(data.msg);
                            press.addClass("active");
                            total = press.children(".num").text();
                            press.children(".num").text(parseInt(total) + 1);

                            total2 = parent.children(".comment-cai").children(".num").text();
                            parent.children(".comment-cai").children(".num").text(parseInt(total2) - 1);
                            parent.children(".comment-cai").removeClass("active");
                        }
                    },
                error: function () {
                    console.log("赞或者踩异常！");
                }
            });
        });
    }

    commentzancai(".comment-zan", 0);
    commentzancai(".comment-cai", 1);
/*回复评论*/
    function replay(){
        /*回复评论*/
        jQuery("[id = replay-comment]").click(function () {
            var replay = $(this);
            var form_name = replay.attr("data-form");
            jQuery("#"+form_name).css("display","block");
        });

        jQuery("[id = submit-replay]").click(function () {
            var replay = $(this);
            var form_name = replay.attr("data-form");
            var pid = replay.attr("data-pid");
            var post_id = replay.attr("data-post");
            var reply_to = replay.attr("data-reply");
            var content = jQuery("#"+form_name+" textarea").val();
            jQuery.ajax({
                type: "post",
                data: {
                    pid: pid,
                    content:content,
                    post_id:post_id,
                    reply_to:reply_to
                },
                url: replay_url,
                dataType: 'json',
                success: function (data) {
                    if (data.status == 1) {
                        document.location.reload();
                    } else if (data.status == -1) {
                        $('#anonymous-dialog').modal('show');
                    }else if(data.status == -2){
                        alert(data.msg);
                    }
                },
                error: function () {
                    console.log("replay error！");
                }
            });
        });

        jQuery("[id = cancel-replay]").click(function () {
            var replay = $(this);
            var form_name = replay.attr("data-form");
            jQuery("#"+form_name).css("display","none")
        });
    }
    replay();


    /*获取标题*/
    jQuery(".get-title").click(function(){
        var url = $("#urlform-url").val();
        jQuery.ajax({
            type: "post",
            data: {
                url: url,

            },
            url: get_title_url,
            dataType: 'json',
            success: function (data) {
                if (data.status == 1) {
                    $("#urlform-title").val(data.msg);
                } else if (data.status == -1) {
                    alert(data.msg);
                }
            },
            error: function () {
                console.log("get title error！");
            }
        });
    });


    jQuery(".gbook_box").hover(function(){
        $(this).find(".collect-report").css("display","block");
    },function(){
        $(this).find(".collect-report").css("display","none");
    });
/*删除评论*/
    jQuery(".delete-comment").click(function(){
        var statue = confirm("你确定要删除这个讨论吗?删除后不可恢复?");
        if(!statue){
            return false;
        }
        var id = $(this).attr("data-id");
        console.log(id);
        jQuery.ajax({
            type: "post",
            data: {
                id: id
            },
            url: del_comment_url,
            dataType: 'json',
            success: function (data) {
                if (data.status == 1) {
                    document.location.reload();
                }else if(data.status == -1){
                    alert(data.msg);
                }
            },
            error: function () {
                console.log("replay error！");
            }
        });
    });
    /*弹出*/
    jQuery(".replyuserhead").hover(function(){
        $(this).popover('show');
    },function(){
        $(this).popover('hide');
    });
/*侧栏隐藏显示互换*/
jQuery(".side-user").click(function(){
    jQuery(".sidea").hide();
    jQuery(".sideb").show(500);
    var a = $("#floatbox").offset();
    $(window).scroll(function() {
        var b = $(window).scrollTop();
        if (b > a.top + -70) {
            $("#divfloat").addClass("fixed")
        } else {
            $("#divfloat").removeClass("fixed")
        }
    });
});
jQuery(".side-info").click(function(){
    jQuery(".sideb").hide();
    jQuery(".sidea").show(500);
    var a = $("#floatbox").offset();
    $(window).scroll(function() {
        var b = $(window).scrollTop();
        if (b > a.top + -70) {
            $("#divfloat").addClass("fixed")
        } else {
            $("#divfloat").removeClass("fixed")
        }
    });
});
jQuery("#fucku").click(function(){
    $.ajax({
        cache: true,
        type: "POST",
        url:anonymous_url,
        data:$('#anonymousform').serialize(),// 你的formid
        async: false,
        error: function(request) {
            console.log("Connection error");
        },
        success: function(data) {
            console.log(data);
        }
    });
});
/*end*/


});




jQuery(document).ready(function() {
  //  jQuery('#floatbox').stickUp();
    var a = $("#floatbox").offset();
    $(window).scroll(function() {
        var b = $(window).scrollTop();
        if (b > a.top + -70) {
            $("#divfloat").addClass("fixed")
        } else {
            $("#divfloat").removeClass("fixed")
        }
    });

});


jQuery(".single img").click(function(){
    var img = new Image();
    img.src =$('img').attr("src") ;
    var w = img.width;
    var h = img.height;
    console.log(w);
    if(w < 650){
        $(this).css("width",w+"px");
    }else{
        $(this).css("width","650px");
    }
});

jQuery(".drop").hover(function(){
    $(this).children(".kanban").show();
},function(){
    $(this).children(".kanban").hide();
});

jQuery(".list-tag").click(function(){
    $(".tagslist").children("span").removeClass("active");
    $(this).addClass("active");
    var cid = $(this).attr('data-id');
    $('.cate-cid').val(cid);
});

jQuery(".mymsg").click(function(){
    var id = $(this).attr('data-id');
    jQuery.ajax({
        type: "post",
        data: {
            id: id,

        },
        url: read_url,
        dataType: 'json',
        success: function (data) {
            console.log(data);
        },
        error: function () {
            console.log(" error！");
        }
    });
});
jQuery('.dropdown').hover(function() {
    jQuery(this).addClass('open');
}, function() {
    jQuery(this).removeClass('open');
});
